package com.quiz.quazz.util;

import android.os.AsyncTask;

import com.quiz.quazz.client.TcpClient;
import com.quiz.quazz.client.TcpClient.OnMessageReceived;

public class ConnectTask extends AsyncTask<String, String, TcpClient> {

	private TcpClient mTcpClient;

	private OnMessageReceived listener;

	private String serverIp;

	private int serverPort;

	private String playerName;

	public ConnectTask(String serverIp, int serverPort, String playerName) {
		this.serverIp = serverIp;
		this.serverPort = serverPort;
		this.playerName = playerName;
	}

	@Override
	protected TcpClient doInBackground(String... message) {

		// we create a TCPClient object and
		mTcpClient = TcpClient.getInstance();
		// mTcpClient.setOnMessageReceivedListener(new TcpClient.OnMessageReceived() {
		// @Override
		// // here the messageReceived method is implemented
		// public void messageReceived(String message) {
		// // this method calls the onProgressUpdate
		// publishProgress(message);
		// System.out.println("client " + IpConfigUtil.getIPAddress(true) + ": " + message);
		// }
		// });
		mTcpClient.setOnMessageReceivedListener(listener);
		mTcpClient.run(serverIp, serverPort, this.playerName);

		return null;
	}

	@Override
	protected void onProgressUpdate(String... values) {
		super.onProgressUpdate(values);
		//
		// // in the arrayList we add the messaged received from server
		// arrayList.add(values[0]);
		// // notify the adapter that the data set has changed. This means that new message received
		// // from server was added to the list
		// mAdapter.notifyDataSetChanged();
	}

	public void setOnMessageReceivedListener(OnMessageReceived listener) {
		this.listener = listener;
	}
}